package com.px.user.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.px.common.util.JsonUtil;
import com.px.user.model.ActionModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "user")
@ApiModel(value = "用户对象", description = "用户字段")
@org.hibernate.annotations.Table(appliesTo = "user", comment = "用户表")
@Table(indexes = {@Index(name = "username_index", columnList = "username", unique = true),
        @Index(name = "nickname_index", columnList = "nickname", unique = true)})
public class User {

    @Id
    @ApiModelProperty(value = "主键")
    @NotNull(groups = {ActionModel.Update.class})
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Null(groups = {ActionModel.Insert.class}, message = "删除id字段")
    @Column(unique = true, nullable = false, columnDefinition = "bigint comment '主键'")
    private Long id;

    @Length(min = 3, max = 20, groups = {ActionModel.Insert.class})
    @Column(columnDefinition = "varchar(64) comment '用户名'")
    @ApiModelProperty(value = "用户名(手机号)", example = "198...")
    private String username;

    @ApiModelProperty(value = "简介", example = "我是谁...(10240)")
    @Column(columnDefinition = "varchar(10240) comment '简介'")
    private String intro;

    @NotBlank(groups = {ActionModel.Insert.class})
    @Column(columnDefinition = "json not null comment '角色'")
    @ApiModelProperty(value = "角色：ADMIN,TEACHER", example = "ADMIN,TEACHER")
    private String roles;

    @ApiModelProperty(value = "是机器人?: 1#是,0#否", hidden = true)
    @Column(columnDefinition = "bit default 0 comment '是机器人?: 1#是,0#否'")
    private Integer isRobot;

    @ApiModelProperty(value = "是虚拟账号?: 1#是,0#否")
    @Column(columnDefinition = "bit default 0 comment '是虚拟账号?: 1#是,0#否'")
    private Integer isVirtual;

    @Column(columnDefinition = "varchar(32) comment '电话号码'")
    @ApiModelProperty(value = "电话号码", example = "192...", hidden = true)
    private String phone;

    @ApiModelProperty(value = "密码", example = "xxx")
    @NotBlank(groups = {ActionModel.Insert.class})
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Column(columnDefinition = "varchar(128) comment '密码'")
    @Length(min = 5, max = 64, groups = {ActionModel.Insert.class})
    private String password;

    @ApiModelProperty(value = "金额", example = "@不提交")
    @Column(columnDefinition = "decimal(12,2) default 0 comment '金额'")
    private BigDecimal money;

    @ApiModelProperty(value = "性别:-1#女,0#未知,1#男", example = "0")
    @Column(columnDefinition = "tinyint default 0 comment '性别:-1#女,0#未知,1#男'")
    private Byte sex;

    @NotBlank(groups = {ActionModel.Insert.class})
    @ApiModelProperty(value = "昵称", example = "冯宝宝")
    @Column(columnDefinition = "varchar(128) comment '昵称'")
    private String nickname;

    @ApiModelProperty(value = "头像", example = "http://...", hidden = true)
    @Column(columnDefinition = "varchar(512) comment '头像'")
    private String avatar;

    @Column(columnDefinition = "bit default 0 comment '已禁用: 1#禁用,0#未禁用'")
    @ApiModelProperty(value = "已禁用: 1#禁用,0#未禁用", example = "0")
    private Integer disabled;

    @ApiModelProperty(value = "来源", hidden = true)
    @Column(columnDefinition = "varchar(64) comment '来源'")
    private String source;

    @ApiModelProperty(value = "IP地址", hidden = true)
    @Column(columnDefinition = "varchar(64) comment 'IP地址'")
    private String ip;

    @Column(columnDefinition = "bigint comment '被邀请的房间ID'")
    @ApiModelProperty(value = "被邀请的房间ID", example = "3")
    private Long inviteRoomId;

    @ApiModelProperty(value = "真实姓名", example = "真名")
    @Column(columnDefinition = "varchar(128) comment '真实姓名'")
    private String name;

    @ApiModelProperty(value = "银行卡号", example = "998...")
    @Column(columnDefinition = "varchar(128) comment '银行卡号'")
    private String bankCard;

    @ApiModelProperty(value = "开户行地址", example = "开户行地址...")
    @Column(columnDefinition = "varchar(256) comment '开户行地址'")
    private String bankName;

    @Generated(GenerationTime.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP comment '创建时间'", insertable = false, updatable = false)
    private Timestamp createTime;

    @Generated(GenerationTime.INSERT)
    @ApiModelProperty(value = "更新时间", hidden = true)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间'", insertable = false, updatable = false)
    private Timestamp updateTime;

    @Transient
    @ApiModelProperty(value = "当前课程列表(提前五分钟，延后1分钟的筛选)", hidden = true)
    private List<Course> courses;

    @Transient
    @ApiModelProperty(value = "管理员房间列表(仅返回)", hidden = true)
    private List<Long> adminRoomIds;

    public String passwordEncode(String password) {
        return new BCryptPasswordEncoder().encode(password);
    }

    public String roles2json(String roles) {
        return JsonUtil.toJson(Arrays.asList(roles.split("\\s*,\\s*")));
    }

    public String json2roles(String roles) {
        return roles.replace("[", "")
                .replace("]", "")
                .replace("\"", "");
    }

    /**
     * 扩展查询字段
     */
    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询开始时间", hidden = true)
    private Timestamp qstart;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询结束时间", hidden = true)
    private Timestamp qend;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询角色", hidden = true)
    private String qrole;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询排序字段", hidden = true)
    private String orderField;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询开始索引", hidden = true)
    private Integer startIndex;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询返回数量", hidden = true)
    private Integer limit;
}
